package ar.uade.fain.ai.tpo.view.receta;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.layout.GroupLayout;
import org.jdesktop.layout.LayoutStyle;

import ar.uade.fain.ai.tpo.Sistema;
import ar.uade.fain.ai.tpo.dto.CaracteristicaDTO;
import ar.uade.fain.ai.tpo.dto.IngredienteDTO;
import ar.uade.fain.ai.tpo.dto.ProductoDTO;
import ar.uade.fain.ai.tpo.dto.RecetaDTO;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ModificarReceta extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Long idIngrediente = 0L;
	private static Long idReceta = 0L;

	private JLabel nombreLabel;
	private JTextField nombreText;
	private JLabel descripcionLabel;
	private JButton altaRecetaButton;
	private JLabel complejidadLabel;
	private JComboBox complejidadCombo;
	private JTextField tCoccionText;
	private JLabel tCoccionLabel;
	private JTextArea descripcionArea;
	private JScrollPane scroll;
	private JTable ingrediente;
	private JCheckBox alHornojCheckBox1;
	private JTextField idRecetaField;
	private JLabel jLabel1;
	private JButton agregarIngredienteButton;
	private DefaultTableModel ingredienteModel;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				AltaReceta inst = new AltaReceta();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public ModificarReceta() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				nombreLabel = new JLabel();
				nombreLabel.setText("Nombre:");
			}
			{
				nombreText = new JTextField();
			}
			{
				descripcionLabel = new JLabel();
				descripcionLabel.setText("Descripcion:");
			}
			{
				alHornojCheckBox1 = new JCheckBox();
				alHornojCheckBox1.setText("Al Horno");
			}
			{
				jLabel1 = new JLabel();
				jLabel1.setText("idReceta:");
			}
			{
				idRecetaField = new JTextField();
			}
			{
				descripcionArea = new JTextArea();
			}
			{
				tCoccionLabel = new JLabel();
				tCoccionLabel.setText("Tiempo de Coccion (min):");
			}
			{
				tCoccionText = new JTextField();
			}
			{
				ComboBoxModel complejidadComboModel = 
						new DefaultComboBoxModel(
								new String[] { "Alta", "Media","Baja" });
				complejidadCombo = new JComboBox();
				complejidadCombo.setModel(complejidadComboModel);
			}
			{
				complejidadLabel = new JLabel();
				complejidadLabel.setText("Compleijdad:");
			}
			{
				altaRecetaButton = new JButton();
				altaRecetaButton.setText("Aceptar");
				altaRecetaButton.addActionListener(new ActionListener() {

					@SuppressWarnings("unchecked")
					@Override
					public void actionPerformed(ActionEvent arg0) {
						if(!nombreText.getText().isEmpty()){
							final CaracteristicaDTO caracteristicaDTO = new CaracteristicaDTO();
							final RecetaDTO recetaDTO = new RecetaDTO();
							caracteristicaDTO.setDescripcion(descripcionArea.getText());
							caracteristicaDTO.setComplejidad(
									complejidadCombo.getSelectedIndex());//1 alta,2 media, 3 baja
							caracteristicaDTO.setAlHorno(alHornojCheckBox1.isSelected());
							caracteristicaDTO.setTiempoCoccion(
									Double.parseDouble(tCoccionText.getText()));
							caracteristicaDTO.setId(idIngrediente);
							recetaDTO.setCaracteristicaDTO(caracteristicaDTO);
							final List<IngredienteDTO> ingredientes = 
									new ArrayList<IngredienteDTO>();
							for (Vector<String> row : 
								((Vector<Vector<String>>)ingredienteModel.getDataVector())) {
								final ProductoDTO producto = new ProductoDTO();
								producto.setId(Long.valueOf(row.elementAt(0)));
								final IngredienteDTO ingrediente = new IngredienteDTO();
								ingrediente.setProductoDTO(producto);
								ingrediente.setCantidad(Double.valueOf(row.elementAt(1)));
								ingredientes.add(ingrediente);
							}
							recetaDTO.setIngredientes(ingredientes);
							recetaDTO.setId(idReceta);
							recetaDTO.setNombre(nombreText.getText());
							idReceta++;
							Sistema.get().getComidaControlador().altaReceta(recetaDTO);
							ModificarReceta.this.dispose();
						}
					}
					
				});
				

			}
			{
				agregarIngredienteButton = new JButton();
				agregarIngredienteButton.setText("Agregar");
				agregarIngredienteButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						ingredienteModel.addRow(new String[]{"",""});
					}
				});
			}
			{
				scroll = new JScrollPane();
				getContentPane().add(scroll);
				scroll.setBounds(23, 101, 354, 141);
				{
					this.ingredienteModel = new DefaultTableModel();
					ingredienteModel.addColumn("ID Producto");
					ingredienteModel.addColumn("Cantidad");
					ingrediente = new JTable();
					scroll.setViewportView(ingrediente);
					ingrediente.setModel(ingredienteModel);
				}
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(31, 31)
				.add(thisLayout.createParallelGroup()
				    .add(GroupLayout.LEADING, scroll, GroupLayout.PREFERRED_SIZE, 133, GroupLayout.PREFERRED_SIZE)
				    .add(thisLayout.createSequentialGroup()
				        .add(thisLayout.createParallelGroup()
				            .add(GroupLayout.LEADING, idRecetaField, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
				            .add(GroupLayout.LEADING, thisLayout.createSequentialGroup()
				                .add(jLabel1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				                .addPreferredGap(LayoutStyle.RELATED)))
				        .add(54)
				        .add(thisLayout.createParallelGroup(GroupLayout.BASELINE)
				            .add(GroupLayout.BASELINE, nombreLabel, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
				            .add(GroupLayout.BASELINE, nombreText, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE))
				        .add(39)))
				.addPreferredGap(LayoutStyle.UNRELATED, 1, Short.MAX_VALUE)
				.add(thisLayout.createParallelGroup(GroupLayout.BASELINE)
				    .add(GroupLayout.BASELINE, tCoccionText, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
				    .add(GroupLayout.BASELINE, tCoccionLabel, GroupLayout.PREFERRED_SIZE, 11, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.UNRELATED, 1, GroupLayout.PREFERRED_SIZE)
				.add(thisLayout.createParallelGroup(GroupLayout.BASELINE)
				    .add(GroupLayout.BASELINE, complejidadCombo, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .add(GroupLayout.BASELINE, complejidadLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .add(GroupLayout.BASELINE, alHornojCheckBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.add(descripcionLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.add(thisLayout.createParallelGroup()
				    .add(thisLayout.createSequentialGroup()
				        .add(0, 0, GroupLayout.PREFERRED_SIZE)
				        .add(descripcionArea, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE))
				    .add(GroupLayout.LEADING, thisLayout.createSequentialGroup()
				        .add(0, 12, GroupLayout.PREFERRED_SIZE)
				        .add(agregarIngredienteButton, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				        .add(19)
				        .add(altaRecetaButton, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)))
				.add(6));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.add(thisLayout.createParallelGroup()
				    .add(GroupLayout.LEADING, thisLayout.createSequentialGroup()
				        .add(thisLayout.createParallelGroup()
				            .add(GroupLayout.LEADING, tCoccionLabel, GroupLayout.PREFERRED_SIZE, 201, GroupLayout.PREFERRED_SIZE)
				            .add(GroupLayout.LEADING, thisLayout.createSequentialGroup()
				                .add(descripcionLabel, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE)
				                .add(83)))
				        .addPreferredGap(LayoutStyle.RELATED)
				        .add(tCoccionText, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
				        .add(6))
				    .add(thisLayout.createSequentialGroup()
				        .add(thisLayout.createParallelGroup()
				            .add(GroupLayout.LEADING, nombreLabel, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
				            .add(GroupLayout.LEADING, thisLayout.createSequentialGroup()
				                .add(complejidadLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				                .add(6)))
				        .add(thisLayout.createParallelGroup()
				            .add(GroupLayout.LEADING, thisLayout.createSequentialGroup()
				                .add(descripcionArea, GroupLayout.PREFERRED_SIZE, 195, GroupLayout.PREFERRED_SIZE)
				                .addPreferredGap(LayoutStyle.RELATED))
				            .add(thisLayout.createSequentialGroup()
				                .addPreferredGap(descripcionArea, complejidadCombo, LayoutStyle.INDENT)
				                .add(thisLayout.createParallelGroup()
				                    .add(GroupLayout.LEADING, thisLayout.createSequentialGroup()
				                        .add(complejidadCombo, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)
				                        .add(32)
				                        .add(alHornojCheckBox1, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE))
				                    .add(GroupLayout.LEADING, thisLayout.createSequentialGroup()
				                        .add(nombreText, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE)
				                        .add(18))))))
				    .add(GroupLayout.LEADING, thisLayout.createSequentialGroup()
				        .add(jLabel1, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.UNRELATED)
				        .add(idRecetaField, GroupLayout.PREFERRED_SIZE, 147, GroupLayout.PREFERRED_SIZE)
				        .add(16)))
				.add(60)
				.add(thisLayout.createParallelGroup()
				    .add(thisLayout.createSequentialGroup()
				        .add(0, 0, Short.MAX_VALUE)
				        .add(scroll, GroupLayout.PREFERRED_SIZE, 354, GroupLayout.PREFERRED_SIZE))
				    .add(thisLayout.createSequentialGroup()
				        .add(0, 113, Short.MAX_VALUE)
				        .add(thisLayout.createParallelGroup()
				            .add(GroupLayout.LEADING, agregarIngredienteButton, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
				            .add(GroupLayout.LEADING, altaRecetaButton, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE))
				        .add(111)))
				.addContainerGap(24, 24));
			pack();
			this.setSize(726, 349);
		} catch (Exception e) {
			//add your error handling code here
			e.printStackTrace();
		}
	}

}
